package com.token.iot.bratterycharging.gateway.business;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.api.Constant;
import com.token.iot.bratterycharging.gateway.api.Port;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;

/**
 * 查询端口的充电状态
 * 
 * @author chenzhixuan
 *
 */
@Component("DCADuplexProcessor")
public class DCADuplexProcessor extends DuplexProcessor {

	@Override
	public String request(String imei, Map<String, String> params) throws PendingException {
		String c = "DCA";
		String sessionId = IDGeneration.createSeesionID();
		String command = null;
		String port = params.get(Constant.PORT);
		command = c + sessionId + "/" + port;
		loger.info("the send =" + new String(command));
		byte[] cc = ByteUtil.addFlag(command.getBytes());
		context.writeAndFlush(cc);
		return sessionId;

	}

	@Override
	public Port response(String response) throws PendingException {
		String val = response.substring(14);
		String[] params = val.split("#/#");
		Port port = new Port();
		port.setNo(params[0]);
		int time =0;
		try {
			 time = Integer.parseInt(params[1]);
		}catch(Exception e) {
			loger.error(e);
		}
		port.setTime(time);
		port.setPower(params[2]);
		loger.debug(port.toString());
		return port;

	}

}